//
//  YMHomeNavgationBar.m
//  YMMedical
//
//  Created by y s on 2023/7/18.
//

#import "YMHomeNavgationBar.h"
#import "YMHomeChooseUserView.h"
#import "YMAddOrEditTestUserVC.h"
#import "YMMineService.h"


@interface YMHomeNavgationBar ()

@property (nonatomic, strong) UIImageView *imgvLeft;
@property (nonatomic, strong) UILabel *lblName;
@property (nonatomic, strong) UIImageView *imgvArrow;
@property (nonatomic, strong) UIButton *btnLeft;

@property (nonatomic, strong) UIImageView *imgvDevice;
@property (nonatomic, strong) UILabel *lblDevice;
@property (nonatomic, strong) UIButton *btnDevice;
@property (nonatomic, strong) UIImageView *imgvArrow1;

@property (nonatomic, strong) YMHomeChooseUserView *vChooseUser;

@property (nonatomic, strong) YMHomeChooseUserView *vChooseDevice;

@property (nonatomic, strong) NSArray *arrUser;
@property (nonatomic, strong) NSArray *arrNames;

@property (nonatomic, assign) NSInteger index;
@property (nonatomic, assign) NSInteger deviceIndex;

@end

@implementation YMHomeNavgationBar

- (instancetype)init{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark -- UI
- (void)setUI{
    self.backgroundColor = STheme.D9;
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, TOP_BAR_HEIGHT);
    [self addSubview:self.imgvLeft];
    [self addSubview:self.lblName];
    [self addSubview:self.imgvArrow];
    [self addSubview:self.btnLeft];
    
    [self addSubview:self.imgvDevice];
    [self addSubview:self.lblDevice];
    [self addSubview:self.imgvArrow1];
    [self addSubview:self.btnDevice];
    
    [self.imgvLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.size.mas_equalTo(CGSizeMake(22, 22));
        make.bottom.mas_equalTo(-11);
    }];
    [self.lblName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imgvLeft.mas_right).offset([SFitUtil fit:6]);
        make.centerY.equalTo(self.imgvLeft);
    }];
    [self.imgvArrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lblName.mas_right).offset([SFitUtil fit:6]);
        make.centerY.equalTo(self.imgvLeft);
        make.size.mas_equalTo(CGSizeMake(22, 22));
    }];
    [self.btnLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.left.equalTo(self.imgvLeft);
        make.right.equalTo(self.imgvArrow);
        make.height.mas_equalTo(NAV_BAR_HEIGHT);
    }];
    [self.imgvDevice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.imgvLeft);
        make.right.equalTo(self.lblDevice.mas_left).offset([SFitUtil fit:-6]);
        make.size.mas_equalTo(CGSizeMake(22, 22));
        make.left.greaterThanOrEqualTo(self.imgvArrow.mas_right).offset([SFitUtil fit:5]);
    }];
    [self.imgvArrow1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo([SFitUtil fit:-10]);
        make.centerY.equalTo(self.imgvDevice);
        make.size.mas_equalTo(CGSizeMake(22, 22));
    }];
    [self.lblDevice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.imgvArrow1.mas_left).offset([SFitUtil fit:-3]);
        make.centerY.equalTo(self.imgvDevice);
    }];
    [self.btnDevice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.imgvArrow1);
        make.left.equalTo(self.imgvDevice);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(NAV_BAR_HEIGHT);
    }];
}

- (void)refreshData{
    WEAK_SELF;
    [YMMineService requestMineTesteeListDataWithPage:1 needpage:0 success:^(NSArray * _Nonnull arrData) {
        weakSelf.arrUser = arrData;
        if (weakSelf.index > arrData.count-1){
            weakSelf.index = arrData.count-1;
        }
        if (arrData.count > 0){
            NSDictionary *dict = [SDataUtil dictOf:arrData[weakSelf.index]];
            NSString *nameStr = [SDataUtil stringOf:dict[@"8pa55qzbspju5tzn"] valid:@""];
            NSString *idStr = [SDataUtil stringOf:dict[@"id"] valid:@""];
            weakSelf.lblName.text = nameStr;
            if (weakSelf.changeUserBlock){
                weakSelf.changeUserBlock(idStr);
            }
        }else{
            weakSelf.lblName.text = KLanguage(@"un_add_txt");
            if (weakSelf.changeUserBlock){
                weakSelf.changeUserBlock(@"");
            }
        }
        NSMutableArray *arrTemp = [[NSMutableArray alloc] init];
        for (NSDictionary *item in arrData) {
            NSString *name = [SDataUtil stringOf:item[@"8pa55qzbspju5tzn"] valid:@""];
            [arrTemp addObject:name];
        }
        [arrTemp addObject:KLanguage(@"newly_add_txt")];
        if (arrTemp.count > 1){
            weakSelf.imgvArrow.hidden = NO;
        }else{
            weakSelf.imgvArrow.hidden = YES;
        }
        weakSelf.vChooseUser.arrList = [arrTemp copy];
    } failure:^(NSString * _Nonnull message) {
        
    }];
}
- (void)setArrDevice:(NSArray *)arrDevice{
    _arrDevice = arrDevice;
    if (arrDevice != nil){
        NSMutableArray *arrTemp = [[NSMutableArray alloc] init];
        for (NSDictionary *item in arrDevice) {
            NSString *name = [SDataUtil stringOf:item[@"lh00tcgkbbqpj185"] valid:@""];
            [arrTemp addObject:name];
        }
        if (arrTemp.count > 1){
            self.imgvArrow1.hidden = NO;
        }else{
            self.imgvArrow1.hidden = YES;
        }
        self.vChooseDevice.arrList = [arrTemp copy];
    }
    [self updateDeviceInfor];
}

- (void)leftClickAction:(id)sender{
    [self.vChooseUser show];
    
}
- (void)deviceClickAction:(id)sender{
    [self.vChooseDevice show];
}
//更新设备信息
- (void)updateDeviceInfor{
    if (_arrDevice == nil || _arrDevice.count < 1){
        _deviceIndex = 0;
        _lblDevice.text = KLanguage(@"device_txt");
        [YMMyModel shared].deviceId = @"";
        return;
    }
    if (_deviceIndex > self.arrDevice.count-1){
        _deviceIndex = 0;
    }
    NSDictionary *dict = [SDataUtil dictOf:self.arrDevice[_deviceIndex]];
    NSString *deviceSn = [SDataUtil stringOf:dict[@"bhmohipntdlpc61m"] valid:@""];
    NSString *nameStr = [SDataUtil stringOf:dict[@"lh00tcgkbbqpj185"] valid:@""];
    self.lblDevice.text = nameStr;
    
    [YMMyModel shared].deviceId = deviceSn;
}


#pragma mark -- setter and getter
- (UIImageView *)imgvLeft{
    if (_imgvLeft == nil){
        _imgvLeft = [[UIImageView alloc] init];
        _imgvLeft.image = [UIImage imageNamed:@"home_nav_add_user_icon"];
    }
    return _imgvLeft;
}
- (UILabel *)lblName{
    if (_lblName == nil){
        _lblName = [[UILabel alloc] init];
        _lblName.text = KLanguage(@"un_add_txt");
        _lblName.textColor = STheme.D1;
        _lblName.font = [SFitUtil fitBoldFont:16];
    }
    return _lblName;
}
- (UIImageView *)imgvArrow{
    if (_imgvArrow == nil){
        _imgvArrow = [[UIImageView alloc] init];
        _imgvArrow.image = [UIImage imageNamed:@"home_nav_updown_arrow"];
        _imgvArrow.hidden = YES;
    }
    return _imgvArrow;
}
- (UIButton *)btnLeft{
    if (_btnLeft == nil){
        _btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnLeft addTarget:self action:@selector(leftClickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnLeft;
}
- (UIImageView *)imgvDevice{
    if (_imgvDevice == nil){
        _imgvDevice = [[UIImageView alloc] init];
        _imgvDevice.image = [UIImage imageNamed:@"home_nav_device_icon"];
    }
    return _imgvDevice;
}
- (UILabel *)lblDevice{
    if (_lblDevice == nil){
        _lblDevice = [[UILabel alloc] init];
        _lblDevice.text = KLanguage(@"device_txt");
        _lblDevice.textColor = STheme.D1;
        _lblDevice.font = [SFitUtil fitBoldFont:16];
    }
    return _lblDevice;
}
- (UIImageView *)imgvArrow1{
    if (_imgvArrow1 == nil){
        _imgvArrow1 = [[UIImageView alloc] init];
        _imgvArrow1.image = [UIImage imageNamed:@"home_nav_updown_arrow"];
        _imgvArrow1.hidden = YES;
    }
    return _imgvArrow1;
}
- (UIButton *)btnDevice{
    if (_btnDevice == nil){
        _btnDevice = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnDevice addTarget:self action:@selector(deviceClickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnDevice;
}
- (YMHomeChooseUserView *)vChooseUser{
    if (_vChooseUser == nil){
        _vChooseUser = [[YMHomeChooseUserView alloc] init];
        WEAK_SELF;
        _vChooseUser.chooseUserBlock = ^(NSInteger index) {
            if (index < weakSelf.arrUser.count){
                weakSelf.index = index;
                NSDictionary *dict = [SDataUtil dictOf:weakSelf.arrUser[index]];
                NSString *nameStr = [SDataUtil stringOf:dict[@"8pa55qzbspju5tzn"] valid:@""];
                NSString *idStr = [SDataUtil stringOf:dict[@"id"] valid:@""];
                weakSelf.lblName.text = nameStr;
                if (weakSelf.changeUserBlock){
                    weakSelf.changeUserBlock(idStr);
                }
            }else{
                YMAddOrEditTestUserVC *vc = [[YMAddOrEditTestUserVC alloc] init];
                vc.isAddNew = YES;
                [SJumpUtil pushVC:vc animated:YES];
            }
        };
    }
    return _vChooseUser;
}
- (YMHomeChooseUserView *)vChooseDevice{
    if (_vChooseDevice == nil){
        _vChooseDevice = [[YMHomeChooseUserView alloc] init];
        _vChooseDevice.yWidth = [SFitUtil fit:150];
        _vChooseDevice.yPoint = CGPointMake(SCREEN_WIDTH-[SFitUtil fit:155], TOP_BAR_HEIGHT-7);
        WEAK_SELF;
        _vChooseDevice.chooseUserBlock = ^(NSInteger index) {
            weakSelf.deviceIndex = index;
            [weakSelf updateDeviceInfor];
        };
    }
    return _vChooseDevice;
}


@end
